package com.xiaoyu.merchant.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.ui.widget.NormView;


public class AddSpecificationDialog extends Dialog implements View.OnClickListener {
    private TextView mCommitBtn;//确定按钮
    private TextView mAddBtn;//点击添加按钮
    private LinearLayout mNormLayout;//格式信息列表布局

    private boolean iscancelable;//控制点击dialog外部是否dismiss
    private boolean isBackCancelable;//控制物理返回键是否dismiss
    private Context mContext;

    public AddSpecificationDialog(@NonNull Context context, boolean isCancelable, boolean isBackCancelable) {
        super(context, R.style.MyDialog);
        this.iscancelable = isCancelable;
        this.isBackCancelable = isBackCancelable;
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_specification);//这行一定要写在前面
        initView();
        setCancelable(iscancelable);//点击外部不可dismiss
        setCanceledOnTouchOutside(isBackCancelable);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

    }

    private void initView() {
        mCommitBtn = findViewById(R.id.norm_commit_btn);
        mNormLayout = findViewById(R.id.norm_info_layout);
        mAddBtn = findViewById(R.id.norm_add_btn);

        NormView normView = new NormView(mContext);
        mNormLayout.addView(normView);

        mCommitBtn.setOnClickListener(this);
        mAddBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == mCommitBtn){//确定
            getNorm();
        }else if (v == mAddBtn){//添加
            NormView normView = new NormView(mContext);
            mNormLayout.addView(normView);
        }
    }


    /**
     * 获取所有规格拼接字符串
     */
    private void getNorm(){
        for (int i=0;i<mNormLayout.getChildCount();i++){
            NormView normView = (NormView) mNormLayout.getChildAt(i);
            if (TextUtils.isEmpty(normView.getNormInfo())){
                ToastUtil.showToast(mContext,"请补充完整规格信息！");
                return;
            }
            Log.i("xiaoyuu","规格"+(i+1)+"信息："+normView.getNormInfo());
        }
        dismiss();
    }


}
